package org.infodavid.common.model.impl;

import java.util.Locale;

import org.infodavid.common.model.IContentInfo;
import org.infodavid.common.model.ModelFactory;
import org.infodavid.common.model.components.IOwnerDetails;
import org.infodavid.common.model.impl.AbstractImprovedDataObjectImpl;

/**
 * The Class ContentInfoImpl.
 */
public class ContentInfoImpl extends AbstractImprovedDataObjectImpl<Long> implements IContentInfo {

	/** serialVersionUID. */
	private static final long serialVersionUID = 6523279718688638431L;

	/** The owner details. */
	private IOwnerDetails ownerDetails;

	/** The name. */
	private String name;

	/** The locale. */
	private Locale locale;

	/** The mimeType. */
	private String mimeType;

	/**
	 * constructor.
	 * @param keyArg the key
	 * @throws Exception the exception
	 */
	protected ContentInfoImpl(final Long keyArg) throws Exception {
		this();

		setKey(keyArg);
	}

	/**
	 * constructor.
	 * @throws Exception the exception
	 */
	protected ContentInfoImpl() throws Exception {
		super();
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.INamed#getName()
	 */
	public String getName() {
		return name;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.IContentInfo#getMimeType()
	 */
	public String getMimeType() {
		return mimeType;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.IContentInfo#setMimeType(java.lang.String)
	 */
	public void setMimeType(final String value) {
		if (!equals(mimeType, value)) {
			final Object old = mimeType;
			mimeType = value;
			markModified(MIME_TYPE, old, mimeType);
		}
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.INamed#setName(java.lang.String)
	 */
	public void setName(final String value) {
		if (!equals(name, value)) {
			final Object old = name;
			name = value;
			markModified(NAME, old, name);
		}
	}

	/**
	 * Sets the owner details.
	 * @param value the new owner details
	 */
	protected void setOwnerDetails(final IOwnerDetails value) {
		if (!equals(ownerDetails, value)) {
			final Object old = ownerDetails;
			ownerDetails = value;
			markModified(IOwnerDetails.OWNER_DETAILS, old, ownerDetails);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.IContentInfo#setLanguage(java.util.Locale)
	 */
	public void setLanguage(final Locale value) {
		if (!equals(locale, value)) {
			final Object old = locale;
			locale = value;
			markModified(LANGUAGE, old, locale);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.IContentInfo#getOwnerDetails()
	 */
	public IOwnerDetails getOwnerDetails() {
		return ownerDetails;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.IContentInfo#getLocale()
	 */
	public Locale getLanguage() {
		return locale;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#toString(java.lang.StringBuffer)
	 */
	@Override
	protected StringBuffer toString(final StringBuffer builder) {
		super.toString(builder);

		builder.append("[ownerDetails=");
		builder.append(ownerDetails);
		builder.append(", name=");
		builder.append(name);
		builder.append(", mimeType=");
		builder.append(mimeType);
		builder.append(", locale=");

		if (locale == null) {
			builder.append("null");
		}
		else {
			builder.append(locale.getDisplayLanguage());
		}

		builder.append(']');

		return builder;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#init()
	 */
	@Override
	public void init() throws Exception {
		super.init();

		name = null;
		mimeType = null;
		locale = null;
		ownerDetails = ModelFactory.getInstance().create(IOwnerDetails.class);
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.impl.DataObjectImpl#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;

		result = prime * result + (getName() == null ? 0 : getName().hashCode());
		result = prime * result + (getLanguage() == null ? 0 : getLanguage().getLanguage().hashCode());

		return result;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.impl.DataObjectImpl#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj == null) {
			return false;
		}

		if (!(obj instanceof IContentInfo)) {
			return false;
		}

		final IContentInfo other = (IContentInfo)obj;

		if (getName() == null) {
			if (other.getName() != null) {
				return false;
			}
		}
		else if (!getName().equals(other.getName())) {
			return false;
		}

		if (getLanguage() == null) {
			if (other.getLanguage() != null) {
				return false;
			}
		}
		else if (!getLanguage().getLanguage().equals(other.getLanguage().getLanguage())) {
			return false;
		}

		return true;
	}
}
